<template>
    <div>
        <!-- 查询条件开始 -->
        <el-form ref="search" :model="search" :inline="true" label-width="68px">
            <el-form-item label="字典名称" prop="dictName">
                <el-input
                        v-model="search.dictName"
                        placeholder="请输入字典名称"
                        clearable
                        size="small"
                        style="width:240px"/>
            </el-form-item>
            <el-form-item label="字典类型" prop="dictType">
                <el-input
                        v-model="search.dictType"
                        placeholder="请输入字典类型"
                        clearable
                        size="small"
                        style="width:240px"/>
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-select
                        v-model="search.status"
                        placeholder="字典状态"
                        clearable
                        size="small"
                        style="width:240px">
                    <el-option
                            v-for="dict in statusOptions"
                            :key="dict.dictValue"
                            :label="dict.dictLabel"
                            :value="dict.dictValue"/>
                </el-select>
            </el-form-item>
            <el-form-item label="创建时间">
                <el-date-picker
                        v-model="search.dateRange"
                        size="small"
                        style="width:240px"
                        value-format="yyyy-MM-dd"
                        type="daterange"
                        range-separator="-"
                        start-placeholde="开始日期"
                        end-placeholde="结束日期"/>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="searchdata">搜索</el-button>
                <el-button type="primary" icon="el-icon-refresh" size="mini" @click="clearsearch">重置</el-button>
            </el-form-item>
        </el-form>
        <!-- 查询条件结束 -->

        <!-- 表格工具按钮开始 -->
        <el-row :gutter="10" style="margin-bottom: 8px;">
            <el-col :span="1.5">
                <el-button type="primary" icon="el-icon-plus" size="mini" @click="addDictType">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" icon="el-icon-refresh" size="mini" @click="handleCacheAsync">缓存同步</el-button>
            </el-col>
        </el-row>
        <!-- 表格工具按钮结束 -->

        <!-- 数据表格开始 -->
        <el-table  border :data="dictTypeTableList" @selection-change="handleSelectionChnage">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="字典编号" prop="dictId" align="center" />
            <el-table-column label="字典名称" prop="dictName" align="center" :show-overflow-tooltip="true" />
            <el-table-column label="字典类型" prop="dictType" align="center" :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <router-link :to="'/dict/data/' + scope.row.dictId" class="link-type">
                        <span>{{ scope.row.dictType }}</span>
                    </router-link>
                </template>
            </el-table-column>
            <el-table-column label="状态" prop="status" align="center" :formatter="statusFormatter" />
            <el-table-column label="备注" prop="remark" align="center" :show-overflow-tooltip="true" />
            <el-table-column label="创建时间" prop="createTime" align="center" width="180" />
            <el-table-column label="操作" align="center">
                <template slot-scope="scope">
                    <el-button type="text" icon="el-icon-edit" size="mini" @click="handleUpdate(scope.row)">修改</el-button>
                    <el-button type="text" icon="el-icon-delete" size="mini" @click="handleDelete(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 数据表格结束 -->
        <!--  分页-->
        <el-pagination
                v-show="page.total>0"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="page.current"
                :page-sizes="[10, 15, 20, 25]"
                :page-size="page.size"
                layout="total, sizes, prev, pager, next, jumper"
                :total="page.total">
        </el-pagination>
        <!--  分页结束-->

        <!-- 添加修改弹出层开始 -->
        <el-dialog
                :title="title"
                :visible.sync="open"
                width="500px"
                center
                append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="字典名称" prop="dictName">
                    <el-input v-model="form.dictName" placeholder="请输入字典名称" clearable size="small" />
                </el-form-item>
                <el-form-item label="字典类型" prop="dictType">
                    <el-input v-model="form.dictType" placeholder="请输入字典类型" clearable size="small" />
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-radio-group v-model="form.status">
                        <el-radio
                                v-for="dict in statusOptions"
                                :key="dict.dictValue"
                                :label="dict.dictValue"
                                :value="dict.dictValue">{{ dict.dictLabel }}</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" type="textarea" placeholder="请输入字典备注" clearable size="small" />
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </span>
        </el-dialog>
        <!-- 添加修改弹出层结束 -->
    </div>
</template>
<style>
    .demo-table-expand {
        font-size: 0;
    }
    .demo-table-expand label {
        width: 90px;
        color: #99a9bf;
    }
    .demo-table-expand .el-form-item {
        margin-right: 0;
        margin-bottom: 0;
        width: 50%;
    }
</style>
<script>
    export default {
        data() {
            return {
                //状态字典
                statusOptions:[],
                // 非多个禁用
                multiple:true,
                // 选中数组
                ids: [],
                //条件查询
                search:{},
                //分页
                page:{
                    current:1,
                    size:10,
                    total:10
                },
                //表单数据
                dictTypeTableList: [],
                //弹出层
                open:false,
                //弹出层标题
                title:'',
                //查询表单
                form: {},
                // 表单校验
                rules: {
                    dictName: [
                        { required: true, message: '字典名称不能为空', trigger: 'blur' }
                    ],
                    dictType: [
                        { required: true, message: '字典类型不能为空', trigger: 'blur' }
                    ]
                }
            }
        },
        methods: {
            //加载系统状态
            initStatus() {
                this.$axios.get("system/api/dict/data/findByType/sys_normal_disable").then(r => {
                    this.statusOptions = r.data.t;
                })
            },
            //加载字典数据菜单
            initDictData(){
                const params= Object.assign(this.search,this.page);
                this.$axios.post("system/api/dict/typeData/getAllDictList",params).then(r=>{
                    this.dictTypeTableList=r.data.t.records;
                    this.page.total=r.data.t.total;
                })
            },
            //状态格式解析
            statusFormatter(row){
                let val=row.status;
                return this.formatDict(this.statusOptions,val)
            },
            //缓存同步数据redis
            handleCacheAsync(){
                this.$axios.get("/system/api/dict/typeData/cacheAsync").then(result=>{
                    this.$message.success("数据字典同步到redis成功")
                })
            },
            //多选框
            handleSelectionChnage(selection) {
                this.ids = selection.map(item => item.dictId)
                this.multiple = !selection.length
            },
            //条件查询、重置数据
            clearsearch(){
                this.search={};
            },
            //条件查询
            searchdata(){
                //查询完成后回到第一页
                this.page.current=1;
                this.initDictData();
            },
            //每页显示的条数
            handleSizeChange(val) {
                this.page.size=val;
                this.initDictData();
            },
            handleCurrentChange(val) {
                this.page.current=val;
                this.initDictData();
            },
            //添加字典提交表单
            handleSubmit() {
                this.$refs['form'].validate((valid) => {
                    if (valid) {
                        this.$axios.post("system/api/dict/typeData/addDictType",this.form).then(r=>{
                            if (r.data.t){
                                this.$message.success(r.data.msg);
                            }else {
                                this.$message.error("操作失败");
                            }
                            this.initDictData();
                            this.open=false;
                            this.title='';
                            this.form={};
                        })
                    }
                });
            },
            //添加字典重置表单
            cancel() {
                this.open=false;
            },
            //添加字典 1、开启对话框启用添加表单
            addDictType(){
                if (this.$refs.form != undefined) {
                    this.$refs.form.clearValidate();
                }
                this.title="添加字典";
                this.form={status:'0'};
                this.open=true;
            },
            //编辑操作
            handleUpdate(row) {
                if (this.$refs.form != undefined) {
                    this.$refs.form.clearValidate();
                }
                this.title="修改字典信息";
                this.form =JSON.parse(JSON.stringify(row));
                this.form.dictId=row.dictId;
                this.open=true;
            },
            //删除操作
            handleDelete(row) {
                const dictIds = row.dictId || this.ids
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$axios.delete("system/api/dict/typeData/delDictType/"+dictIds).then(r=>{
                        if (r.data.t){
                            this.$message.success(r.data.msg);
                        }else {
                            this.$message.error("删除失败");
                        }
                        this.initDictData();
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            }
        },
        created() {
            //加载系统状态
            this.initStatus();
            //加载字典数据
            this.initDictData();
        }
    }
</script>
